package com.wuyi.tea.controller.common;

import com.wuyi.tea.domain.dto.ArticleDTO;
import com.wuyi.tea.domain.dto.CommentDTO;
import com.wuyi.tea.domain.pojo.Article;
import com.wuyi.tea.domain.pojo.Comment;
import com.wuyi.tea.domain.pojo.Picture;
import com.wuyi.tea.domain.vo.ArticleVO;
import com.wuyi.tea.domain.vo.CommentVO;
import com.wuyi.tea.domain.vo.R;
import com.wuyi.tea.enums.ConstantEnum;
import com.wuyi.tea.service.ArticleService;
import com.wuyi.tea.service.CommentService;
import com.wuyi.tea.service.PictureService;
import com.wuyi.tea.util.LocalStorageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Slf4j
@Api(tags = "茶友圈模块")
@RestController
@RequestMapping("/article")
@Transactional
public class TeaFriendChatController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private PictureService pictureService;
    @Autowired
    private CommentService commentService;


    @ApiOperation("发布文章")
    @PostMapping
    public R releaseArticle(@RequestBody ArticleDTO articleDTO){
        log.info("id为{}的用户发布了一条文章,\n文章：{}",LocalStorageUtil.get(),articleDTO);
        articleService.releaseByarticle(articleDTO);
        return R.success();
    }

    @ApiOperation("删除文章")
    @DeleteMapping
    public R deleteArticle(@RequestParam Long id){
        log.info("id为{}的用户删除了一条文章,id: id", LocalStorageUtil.get(),id);
        articleService.deleteArticle(id);
        return R.success();
    }


    @ApiOperation("查看文章列表")
    @GetMapping("/list")
    public R getArticleList(){
        log.info("id为{}的用户查看了所有的文章",LocalStorageUtil.get());
        List<ArticleVO> articleVOList=articleService.getArticleVoList();
        return R.success(articleVOList);
    }


    @ApiOperation("给文章点赞")
    @PostMapping("/like")
    public R likeArticle(@RequestParam Long articleId){
        log.info("id为{}的用户给文章点赞",LocalStorageUtil.get());
        articleService.addLikeArticle(articleId);
        return R.success();
    }

    @ApiOperation("发布评论")
    @PostMapping("/comment")
    public R releaseArticleComment(@RequestParam Long articleId,@RequestBody CommentDTO commentDTO){
        log.info("id为{}的用户发布了评论,文章id: {}, 评论信息：{}",LocalStorageUtil.get(), articleId, commentDTO);
        commentService.releaseArticleComment(articleId,commentDTO);
        return R.success();
    }

    @ApiOperation("删除评论")
    @DeleteMapping("/comment")
    public R deleteArticleComment(@RequestParam Long commentId){
        log.info("id为{}的员工删除了评论,评论id: {}",LocalStorageUtil.get(),commentId);
        commentService.deleteArticleComment(commentId);
        return R.success();
    }

    @ApiOperation("查看某文章的所有评论")
    @GetMapping("/comment/list")
    public R getArticleCommentList(@RequestParam Long articleId){
        log.info("id为{}的员共查看了文章id为{}的所有评论",LocalStorageUtil.get(),articleId);
        List<CommentVO> commentlist=commentService.getArticleAllCommentList(articleId);
        return R.success(commentlist);
    }


    @ApiOperation("给评论点赞")
    @PostMapping("/comment/like")
    public R likeArticleComment(@RequestParam Long articleId){
        log.info("id为{}的用户给评论点赞了,文章id: {}",LocalStorageUtil.get(),articleId);
        commentService.addlikeArticleComment(articleId);
        return R.success();
    }

}
